require("../../assets/css/basic.less");
require("../../assets/css/normalize.css");
require("../../assets/css/property.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/font-icon/iconfont.css");
require("../sporting/sporting.less");
const data = require("../../libs/data.js");
const dom = require("../../libs/dom.js");
const axios = require("../../libs/axios.js");

document.addEventListener("DOMContentLoaded", function () {
	// 高德地图
	// 1.地理定位
	AMap.plugin("AMap.Geolocation", function () {
		var geolocation = new AMap.Geolocation({
			// 是否使用高精度定位，默认：true
			enableHighAccuracy: true,
			// 设置定位超时时间，默认：无穷大
			timeout: 10000,
			// 定位按钮的停靠位置的偏移量
			offset: [10, 20],
			//  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
			zoomToAccuracy: true,
			//  定位按钮的排放位置,  RB表示右下
			position: "RB",
		});

		geolocation.getCurrentPosition(function (status, result) {
			if (status == "complete") {
				onComplete(result);
				console.log("定位成功");
			} else {
				onError(result);
				console.log("定位失败");
			}
		});

		function onComplete(data) {
			// data是具体的定位信息
		}

		function onError(data) {
			// 定位出错
		}
	});

	// 2.计算距离
	var p1 = [116.434027, 39.941037];
	var p2 = [116.461665, 39.941564];
	// 返回 p1 到 p2 间的地面距离，单位：米
	var dis = AMap.GeometryUtil.distance(p1, p2);

	// 3.定时器模拟跑步数据
	let duration = 0; //设置持续时间
	let speed = 0; //设置速度
	let meter = 0; //总里程
	let timer = null;

	function start() {
		timer = setInterval(function () {
			// 速度
			let speed = getRand(3, 7);
			dom.get("#speed").textContent = speed;

			// 时间
			duration++;
			dom.get("#time").textContent = data.getTime(duration);
			// 卡路里
			calorie = 60 * (meter / 1000) * 1.036;
			dom.get("#calorie").textContent = calorie.toFixed(3);
			// 总里程
			meter += speed;
			dom.get("#distance").textContent = (meter / 1000).toFixed(3);
		}, 100);
	}
	start();
	// 获取两个整数之间的随机数
	function getRand(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}

	dom.get(".parse").style.display = "flex";
	dom.get(".continue").style.display = "none";
	dom.get(".stop").style.display = "none";

	// 暂停按钮
	dom.get(".parse").addEventListener("click", function () {
		dom.getAll(".time-out div").forEach(function (v) {
			v.style.display = "none";
		});
		dom.getAll(".time-out div")[0].style.display = "none";
		dom.getAll(".time-out div")[1].style.display = "flex";
		dom.getAll(".time-out div")[2].style.display = "flex";
		dom.getAll(".time-out div")[3].style.display = "none";
		clearInterval(timer);
	});
	// 继续按钮
	dom.get(".continue").addEventListener("click", function () {
		dom.getAll(".time-out div").forEach(function (v) {
			v.style.display = "none";
		});
		dom.getAll(".time-out div")[0].style.display = "flex";
		dom.getAll(".time-out div")[1].style.display = "none";
		dom.getAll(".time-out div")[2].style.display = "none";
		dom.getAll(".time-out div")[3].style.display = "block";
		start();
	});
	// 结束按钮
	dom.get(".stop").addEventListener("click", function () {
		axios
			.post("/api/exercise/save", {
				type: 0,
				duration,
				calorie,
				meter,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					alert("跑步结束,下次再跑!!!");
					location.href = "sporting-stop.html";
				}
			});
	});
});
